<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryFormRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="deployTime">
        <el-date-picker
          clearable
          v-model="queryParams.deployTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="选择时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" size="default" @click="handleQuery">搜索</el-button>
        <el-button :icon="Refresh" size="default" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          :icon="IconDelete"
          size="default"
          :disabled="multiple"
          @click="handleDelete()" 
          v-hasPermi="['system:deployment:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="finishedList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" />
      <el-table-column label="任务编号" align="center" prop="taskId" :show-overflow-tooltip="true"/>
      <el-table-column label="流程名称" align="center" prop="procDefName" :show-overflow-tooltip="true"/>
      <el-table-column label="任务节点" align="center" prop="taskName"/>
      <el-table-column label="流程发起人" align="center">
        <template #default="scope">
          <label v-if="scope.row.startUserName">{{scope.row.startUserName}} <el-tag type="info" size="default">{{scope.row.startDeptName}}</el-tag></label>
        </template>
      </el-table-column>
      <el-table-column label="接收时间" align="center" prop="createTime"/>
      <el-table-column label="审批时间" align="center" prop="finishTime"/>
      <el-table-column label="耗时" align="center" prop="duration"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="default"
            type="text"
            :icon="Tickets"
            @click="handleFlowRecord(scope.row)"
          >流转记录
          </el-button>
          <el-button
            size="default"
            type="text"
            :icon="RefreshLeft"
            @click="handleRevoke(scope.row)"
          >撤回
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup>
import { getFinishedList, delDeployment, revokeProcess } from "@/api/flowable/finished";
import { Search, Refresh, Tickets, RefreshLeft, Delete as IconDelete } from '@element-plus/icons-vue';
import { ref, reactive, onMounted, getCurrentInstance } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';

const { proxy } = getCurrentInstance();
const router = useRouter();

const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const finishedList = ref([]);

const ids = ref([]);
const single = ref(true);
const multiple = ref(true);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  name: null,
  deployTime: null,
});

const queryFormRef = ref(null);

/** 查询已办任务列表 */
function getList() {
  loading.value = true;
  getFinishedList(queryParams).then(response => {
    finishedList.value = response.data.records;
    total.value = response.data.total;
    loading.value = false;
  }).catch(() => {
    loading.value = false;
  });
}

function handleFlowRecord(row) {
  router.push({
    path: '/flowable/task/finished/detail/index',
    query: {
      procInsId: row.procInsId,
      deployId: row.deployId,
      taskId: row.taskId,
    }
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryFormRef.value?.resetFields();
  handleQuery();
}

function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.taskId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

function handleDelete(row) {
  const deleteIds = row && row.taskId ? [row.taskId] : ids.value;
  if (deleteIds.length === 0) {
    ElMessage.warning("请选择要删除的数据");
    return;
  }
  ElMessageBox.confirm('是否确认删除任务编号为"' + deleteIds.join(',') + '"的数据项?', "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    return delDeployment(deleteIds.join(','));
  }).then(() => {
    getList();
    ElMessage.success("删除成功");
  }).catch(() => {});
}

function handleRevoke(row) {
  const params = {
    instanceId: row.procInsId
  };
  ElMessageBox.confirm('确认撤回该任务吗？', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(function() {
    return revokeProcess(params);
  }).then((res) => {
    ElMessage.success(res.msg || "撤回成功");
    getList();
  }).catch(() => {});
}

onMounted(() => {
  getList();
});
</script>

<style scoped>
.mb8 {
  margin-bottom: 8px;
}
</style>

